home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cc / cp-errfn.c < prev    next >
C/C++ Source or Header  |  1993-12-02  |  5KB  |  219 lines

  1. /* Provide a call-back mechanism for handling error output.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.    Contributed by Jason Merrill (jason@cygnus.com)
  4.  
  5.    This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.    
  21. #include "config.h"
  22. #include "tree.h"
  23. #include <ctype.h>
  24.  
  25. /* cp_printer is the type of a function which converts an argument into
  26.    a string for digestion by printf.  The cp_printer function should deal
  27.    with all memory management; the functions in this file will not free
  28.    the char*s returned.  See cp-error.c for an example use of this code.  */
  29.  
  30. typedef char* cp_printer PROTO((HOST_WIDE_INT, int));
  31. extern cp_printer * cp_printers[256];
  32.  
  33. typedef void errorfn ();    /* deliberately vague */
  34.  
  35. extern char* cp_file_of PROTO((tree));
  36. extern int   cp_line_of PROTO((tree));
  37.  
  38. #define STRDUP(f) (ap = (char *) alloca (strlen (f) +1), strcpy (ap, (f)), ap)
  39.  
  40. #define NARGS 3
  41. #define arglist a1, a2, a3
  42. #define arglist_dcl HOST_WIDE_INT a1, a2, a3;
  43. #define ARGSINIT args[0] = a1; args[1] = a2; args[2] = a3;
  44. #define ARGSLIST args[0], args[1], args[2]
  45.  
  46. static void
  47. cp_thing (errfn, atarg1, format, arglist)
  48.      errorfn *errfn;
  49.      int atarg1;
  50.      char *format;
  51.      arglist_dcl
  52. {
  53.   char *fmt;
  54.   char *f;
  55.   char *ap;
  56.   int arg;
  57.   HOST_WIDE_INT atarg = atarg1 ? a1 : 0;
  58.   HOST_WIDE_INT args[NARGS];
  59.   ARGSINIT
  60.  
  61.   fmt = STRDUP(format);
  62.   
  63.   for (f = fmt, arg = 0; *f; ++f)
  64.     {
  65.       cp_printer * function;
  66.       int alternate;
  67.       int maybe_here;
  68.       
  69.       /* ignore text */
  70.       if (*f != '%') continue;
  71.  
  72.       ++f;
  73.  
  74.       alternate = 0;
  75.       maybe_here = 0;
  76.  
  77.       /* ignore most flags */
  78.       while (*f == ' ' || *f == '-' || *f == '+' || *f == '#')
  79.     {
  80.       if (*f == '+')
  81.         maybe_here = 1;
  82.       else if (*f == '#')
  83.         alternate = 1;
  84.       ++f;
  85.     }
  86.  
  87.       /* ignore field width */
  88.       if (*f == '*')
  89.     {
  90.       ++f;
  91.       ++arg;
  92.     }
  93.       else
  94.     while (isdigit (*f))
  95.       ++f;
  96.  
  97.       /* ignore precision */
  98.       if (*f == '.')
  99.     {
  100.       ++f;
  101.       if (*f == '*')
  102.         {
  103.           ++f;
  104.           ++arg;
  105.         }
  106.       else
  107.         while (isdigit (*f))
  108.           ++f;
  109.     }
  110.  
  111.       /* ignore "long" */
  112.       if (*f == 'l')
  113.     ++f;
  114.  
  115.       function = cp_printers[*f];
  116.  
  117.       if (function)
  118.     {
  119.       char *p;
  120.  
  121.       if (arg >= NARGS) abort ();
  122.       
  123.       if (maybe_here && atarg)
  124.         atarg = args[arg];
  125.  
  126.       /* Must use a temporary to avoid calling *function twice */
  127.       p = (*function) (args[arg], alternate);
  128.       args[arg] = (HOST_WIDE_INT) STRDUP(p);
  129.       *f = 's';
  130.     }
  131.  
  132.       ++f;
  133.       ++arg;            /* Assume valid format string */
  134.  
  135.     }
  136.  
  137.   if (atarg)
  138.     {
  139.       char *file = cp_file_of ((tree) atarg);
  140.       int   line = cp_line_of ((tree) atarg);
  141.       (*errfn) (file, line, fmt, ARGSLIST);
  142.     }
  143.   else
  144.     (*errfn) (fmt, ARGSLIST);
  145.  
  146. }
  147.  
  148. void
  149. cp_error (format, arglist)
  150.      char *format;
  151.      arglist_dcl
  152. {
  153.   extern errorfn error;
  154.   cp_thing (error, 0, format, arglist);
  155. }
  156.  
  157. void
  158. cp_warning (format, arglist)
  159.      char *format;
  160.      arglist_dcl
  161. {
  162.   extern errorfn warning;
  163.   cp_thing (warning, 0, format, arglist);
  164. }
  165.  
  166. void
  167. cp_pedwarn (format, arglist)
  168.      char *format;
  169.      arglist_dcl
  170. {
  171.   extern errorfn pedwarn;
  172.   cp_thing (pedwarn, 0, format, arglist);
  173. }
  174.  
  175. void
  176. cp_compiler_error (format, arglist)
  177.      char *format;
  178.      arglist_dcl
  179. {
  180.   extern errorfn compiler_error;
  181.   cp_thing (compiler_error, 0, format, arglist);
  182. }
  183.  
  184. void
  185. cp_sprintf (format, arglist)
  186.      char *format;
  187.      arglist_dcl
  188. {
  189.   extern errorfn sprintf;
  190.   cp_thing (sprintf, 0, format, arglist);
  191. }
  192.  
  193. void
  194. cp_error_at (format, arglist)
  195.      char *format;
  196.      arglist_dcl
  197. {
  198.   extern errorfn error_with_file_and_line;
  199.   cp_thing (error_with_file_and_line, 1, format, arglist);
  200. }
  201.  
  202. void
  203. cp_warning_at (format, arglist)
  204.      char *format;
  205.      arglist_dcl
  206. {
  207.   extern errorfn warning_with_file_and_line;
  208.   cp_thing (warning_with_file_and_line, 1, format, arglist);
  209. }
  210.  
  211. void
  212. cp_pedwarn_at (format, arglist)
  213.      char *format;
  214.      arglist_dcl
  215. {
  216.   extern errorfn pedwarn_with_file_and_line;
  217.   cp_thing (pedwarn_with_file_and_line, 1, format, arglist);
  218. }
  219.